<template>
  <div>
    <!--引用表格-->
   <BasicTable @register="registerTable" :rowSelection="rowSelection"  @expand="onExpand">
     <!--插槽:table标题-->
      <template #tableTitle>
          <a-button type="primary" v-auth="'dw:dw_com_emp:add'" @click="handleAdd" preIcon="ant-design:plus-outlined"> 新增</a-button>
          <a-button  type="primary" v-auth="'dw:dw_com_emp:exportXls'" preIcon="ant-design:export-outlined" @click="onExportXls"> 导出</a-button>
          <j-upload-button type="primary" v-auth="'dw:dw_com_emp:importExcel'" preIcon="ant-design:import-outlined" @click="onImportXls">导入</j-upload-button>
          <a-dropdown v-if="selectedRowKeys.length > 0">
              <template #overlay>
                <a-menu>
                  <a-menu-item key="1" @click="batchHandleDelete">
                    <Icon icon="ant-design:delete-outlined"></Icon>
                    删除
                  </a-menu-item>
                </a-menu>
              </template>
              <a-button v-auth="'dw:dw_com_emp:deleteBatch'">批量操作
                <Icon icon="mdi:chevron-down"></Icon>
              </a-button>
        </a-dropdown>
        <!-- 高级查询 -->
        <super-query :config="superQueryConfig" @search="handleSuperQuery" />
      </template>
       <!--操作栏-->
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" :dropDownActions="getDropDownAction(record)"/>
      </template>

     <template #expandedRowRender="{ record }"  >
       <a-button type="primary" v-auth="'dw:dw_com_emp:add'" @click="childHandleAdd(record)" preIcon="ant-design:plus-outlined"> 新增工序</a-button>
       <a-dropdown v-if="selectedRowKeyss.length > 0">
         <template #overlay>
           <a-menu>
             <a-menu-item key="1" @click="batchHandleDelete">
               <Icon icon="ant-design:delete-outlined"></Icon>
               删除
             </a-menu-item>
           </a-menu>
         </template>
         <a-button v-auth="'dw:dw_com_emp:deleteBatch'">批量操作
           <Icon icon="mdi:chevron-down"></Icon>
         </a-button>
       </a-dropdown>
       <BasicTable @register="registerInnerTable" :rowSelection="rowSelection12"   :searchInfo={record}>
         <!--操作栏-->
         <template #action="{ record }">

           <!--操作栏-->
           <TableAction :actions="getchildTableAction(record)" :dropDownActions="getChildDropDownAction(record)"/>
         </template>


       </BasicTable>
     </template>


      <!--字段回显插槽-->
      <template v-slot:bodyCell="{ column, record, index, text }">
      </template>
    </BasicTable>
    <!-- 表单区域 -->

    <DwComEmpModal @register="registerModal" @success="handleSuccess"></DwComEmpModal>
    <DwComEmpModal @register="registerDwComEmpModalModal" @success="handleSuccessChild"></DwComEmpModal>
  </div>
</template>

<script lang="ts" name="dw-dwComEmp" setup>
  import {ref, reactive, computed, unref} from 'vue';
  import {BasicTable, useTable, TableAction} from '/@/components/Table';
  import {useModal} from '/@/components/Modal';
  import { useListPage } from '/@/hooks/system/useListPage'
  import DwComEmpModal from './components/DwComEmpModal.vue'
  import {columns, searchFormSchema, superQuerySchema} from './DwComEmp.data';
  import {innerColumns} from './DwComEmp.data';
  import {list, childlist,deleteOne, batchDelete, getImportUrl,getExportUrl} from './DwComEmp.api';
  import { useUserStore } from '/@/store/modules/user';
  import {deleteOneChild} from "@/views/dw/DwProPlan.api";
  const queryParam = reactive<any>({});
  const checkedKeys = ref<Array<string | number>>([]);
  const userStore = useUserStore();
  //注册model
  const [registerModal, {openModal}] = useModal();
  //注册table数据
  const { prefixCls,tableContext,onExportXls,onImportXls } = useListPage({
      tableProps:{
           title: '装配核算',
           api: list,
           columns,
           canResize:false,
        expandRowByClick: true,
           formConfig: {
              //labelWidth: 120,
              schemas: searchFormSchema,
              autoSubmitOnEnter:true,
              showAdvancedButton:true,
              fieldMapToNumber: [
              ],
              fieldMapToTime: [
              ],
            },
           actionColumn: {
               width: 120,
               fixed:'right'
            },
            beforeFetch: (params) => {
              return Object.assign(params, queryParam);
            },
      },
       exportConfig: {
            name:"dw_com_emp",
            url: getExportUrl,
            params: queryParam,
          },
          importConfig: {
            url: getImportUrl,
            success: handleSuccess
          },
  })
  // 注册子表model
  const [registerDwComEmpModalModal, { openModal: openDwAssembleModal }] = useModal();
  // 子表
  const { tableContext: transactionTable } = useListPage({
    tableProps: {
      title: '',
      api: childlist,
      columns:innerColumns,
      canResize: false,
      useSearchForm: false,
      pagination:false,
      expandRowByClick: true,
      striped:true,
      beforeFetch: (params) => {
        let param = {empId:params.record.empId}
        return Object.assign(params, param);
      },
    },

  });
  const [registerTable, {reload},{ rowSelection, selectedRowKeys }] = tableContext
  const [registerInnerTable,{reload:reload1},{ rowSelection:rowSelection12, selectedRowKeys:selectedRowKeyss }] = transactionTable;
  // 高级查询配置
  const superQueryConfig = reactive(superQuerySchema);
  /**
   * 高级查询事件
   */
  function handleSuperQuery(params) {
    Object.keys(params).map((k) => {
      queryParam[k] = params[k];
    });
    reload();
  }
   /**
    * 新增事件
    */
  function handleAdd() {
     openModal(true, {
       isUpdate: false,
       showFooter: true,
     });
  }
   /**
    * 编辑事件
    */
  function handleEdit(record: Recordable) {
     openModal(true, {
       record,
       isUpdate: true,
       showFooter: true,
     });
   }
   /**
    * 详情
   */
  function handleDetail(record: Recordable) {
     openModal(true, {
       record,
       isUpdate: true,
       showFooter: false,
     });
   }
   /**
    * 删除事件
    */
  async function handleDelete(record) {
     await deleteOne({id: record.id}, handleSuccess);
   }
   /**
    * 批量删除事件
    */
  async function batchHandleDelete() {
     await batchDelete({ids: selectedRowKeys.value}, handleSuccess);
   }
   /**
    * 成功回调
    */
  function handleSuccess() {
      (selectedRowKeys.value = []) && reload();
   }
   /**
      * 操作栏
      */
  function getTableAction(record){
       return [
         {
           label: '编辑',
           onClick: handleEdit.bind(null, record),
           auth: 'dw:dw_com_emp:edit'
         }
       ]
   }
     /**
        * 下拉操作栏
        */
  function getDropDownAction(record){
       return [
         {
           label: '详情',
           onClick: handleDetail.bind(null, record),
         }, {
           label: '删除',
           popConfirm: {
             title: '是否确认删除',
             confirm: handleDelete.bind(null, record),
             placement: 'topLeft',
           },
           auth: 'dw:dw_com_emp:delete'
         }
       ]
   }
/** ****************************************************************** */
function getchildTableAction(record){
  return [
    {
      label: '编辑',
      onClick: childHandleEdit.bind(null, record),
      // auth: 'dw:dw_pro_plan:edit'
    }
  ]
}
  /**
   * 子编辑事件
   */
  function childHandleEdit(record: Recordable) {
    openDwAssembleModal(true, {
      record,
      isUpdate: true,
      showFooter: true,
    });
  }
  /**
   * 子新增事件
   */
  function childHandleAdd(record: Recordable) {
    openDwAssembleModal(true, {
      record:{proPlanId:record.id},
      isUpdate: false,
      showFooter: true,
    });
  }
  /**
   * 子详情
   */
  function childHandleDetail(record: Recordable) {
    openDwAssembleModal(true, {
      record,
      isUpdate: true,
      showFooter: false,
    });
  }
  /**
   * 子删除事件
   */
  async function childHandleDelete(record) {
    await deleteOneChild({id: record.id}, handleSuccessChild);
  }
  /**
   * 下拉操作栏
   */
  function getChildDropDownAction(record){
    return [
      {
        label: '详情',
        onClick: childHandleDetail.bind(null, record),
      }, {
        label: '删除',
        popConfirm: {
          title: '是否确认删除',
          confirm: childHandleDelete.bind(null, record),
          placement: 'topLeft',
        },
        auth: 'dw:dw_pro_plan:delete'
      }
    ]
  }

  /**
   * 成功回调
   */
  function handleSuccessChild() {
    // console.log("sssssss:")
    (selectedRowKeyss.value = []) && reload1() && reload();
  }

  async function onExpand( ) {
    handleSuccessChild();
  }






</script>

<style lang="less" scoped>
  :deep(.ant-picker),:deep(.ant-input-number){
    width: 100%;
  }
</style>
